package com.ruoyi.project.beautyflower.member.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.project.beautyflower.BfUtil;
import com.ruoyi.project.beautyflower.order.service.IOrderService;
import com.ruoyi.project.beautyflower.paymentHistory.service.IPaymentHistoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.beautyflower.member.domain.Member;
import com.ruoyi.project.beautyflower.member.service.IMemberService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

/**
 * 用户管理 信息操作处理
 *
 * @author lxs
 * @date 2018-09-28
 */
@Controller
@RequestMapping("/beautyflower/member")
public class MemberController extends BaseController {
    private String prefix = "beautyflower/member";

    @Autowired
    private IMemberService memberService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IPaymentHistoryService paymentHistoryService;

    @RequiresPermissions("beautyflower:member:view")
    @GetMapping()
    public String member() {
        return prefix + "/member";
    }

    /**
     * 查询用户管理列表
     */
    @RequiresPermissions("beautyflower:member:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Member member) {
        startPage();
        List<Member> list = memberService.selectMemberList(member);
        return getDataTable(list);
    }

    /**
     * 新增用户管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存用户管理
     */
    @RequiresPermissions("beautyflower:member:add")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Member member) {
        return toAjax(memberService.insertMember(member));
    }

    /**
     * 修改用户管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        Member member = memberService.selectMemberById(id);
        mmap.put("member", member);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户管理
     */
    @RequiresPermissions("beautyflower:member:edit")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Member member) {
        return toAjax(memberService.updateMember(member));
    }

    /**
     * 删除用户管理
     */
    @RequiresPermissions("beautyflower:member:remove")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(memberService.deleteMemberByIds(ids));
    }

    /**
     * 修改用户管理
     */
    @GetMapping("/details/{id}")
    public String details(@PathVariable("id") Integer id, ModelMap mmap) {
        Object data = BfUtil.queryCreditLineResult(String.valueOf(id)).getData();
        if (data != null) mmap.putAll((Map<String, Object>) data);
        mmap.put("member", memberService.selectMemberById(id));
        mmap.put("loanTotal", orderService.getLoanTotal(String.valueOf(id)));
        mmap.put("paymentTotal", paymentHistoryService.getPaymentTotal(String.valueOf(id)));
        return prefix + "/details";
    }
}
